package com.example.community.list;

import com.example.community.dao.*;
import com.example.community.model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/userManagerList")
public class UserManagerListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer ys = null;
        if (req.getParameter("ys")==null){
            ys = 1 ; //当前页数
        }else {
            ys = Integer.parseInt(req.getParameter("ys"));
        }

        int shuju = 8; //每页显示数据个数
        UsersDao usersDao = new UsersDao();
        List<Users> users = usersDao.fandLimit(ys,shuju);
        for (Users users1:users){
            Integer usNo = users1.getUsNo();
            ParkingDao parkingDao = new ParkingDao();
            try {
                Integer i = parkingDao.selectPno(usNo);
                users1.setpNo(i);
            }catch (Exception e){
//                users1.setpNo(null);
//                e.printStackTrace();
            }
            try {
                users1.setUsAddress(getUsAddress(usNo));
            }catch (Exception e){
//                e.printStackTrace();
            }

        }
        Integer totalPageNum  = usersDao.selectLimitCount(shuju);
        req.setAttribute("userManagerList",users);
        req.setAttribute("totalPageNum",totalPageNum);
        req.setAttribute("ys",ys);
        req.getRequestDispatcher("/WEB-INF/userManager/userList.jsp").forward(req,resp);
    }
    private String getUsAddress(Integer usNo){
        RoomDao roomDao = new RoomDao();
        Room room = roomDao.fandByUsNo(usNo);
        Integer rNo = room.getrNo();
        if (rNo != null){
            Integer unNo = room.getUnNo();
            UnitDao unitDao = new UnitDao();
            Unit unit = unitDao.fandById(unNo);
            Integer fNo = unit.getfNo();
            FloorDao floorDao = new FloorDao();
            Floor floor = floorDao.fandById(fNo);
            Integer ctNo = floor.getCtNo();
            ComtyDao comtyDao = new ComtyDao();
            Comty comty = comtyDao.fandById(ctNo);
            String address = "" + comty.getCtName() + floor.getfName() + unit.getUnName() + room.getrId();
            return address;
        }else return null;
    }
}
